# -*- coding: utf-8 -*-
"""
模块介绍:

创建者:

创建日期:
"""
import json
import docker
from config.settings import *
import pyrestful.rest
from pyrestful import mediatypes
from pyrestful.rest import get, post, put, delete
from Model.DockerModels import Images, Labels
from sqlalchemy import create_engine, distinct
from sqlalchemy.orm import sessionmaker

from Common.utils import (
    AlchemyJsonEncoder,
    log_hande,
    set_http_head,
    ParamikoClient)
logger = logging.getLogger('docker_cloud_manage_info')

# engine = create_engine("mysql+mysqldb://root:qwer121212@localhost:3306/docker_cloud_manage_local?charset=utf8",encoding="utf-8")
engine = create_engine(
    "mysql+mysqldb://admin:qwer121212.@192.168.43.116:3306/docker_cloud_manage?charset=utf8",
    encoding="utf-8",
    # echo=True
)
SessionCls = sessionmaker(bind=engine)
session = SessionCls()


class ServiceCore(pyrestful.rest.RestHandler):

    @set_http_head
    @get(
        _path="/docker_monarch/docker_node_containers",
        _types=[str],
        _produces=mediatypes.APPLICATION_JSON)
    def get_docker_node_containers(self):
        """
        docker 节点 容器列表
        protocol + '://' + ipv4 + port == tcp://192.168.43.116:9527

        测试

        GET http://localhost:8080/docker_monarch/docker_node_containers?
                                                            protocol=tcp&ipv4=192.168.43.116&port=9527&version=&timeout=

        :return:
				{
				  "Command": "/bin/bash",
				  "Created": 1514543367,
				  "HostConfig": {
				    "NetworkMode": "default"
				  },
				  "Id": "5b39a525f14c48495322820191dfc63b0889a703a167402bec296b27de491546",
				  "Image": "3fa822599e10",
				  "ImageID": "sha256:3fa822599e10c5f2080dcf647068c72022b111d31bbec0c5adb8a96e7eb5379b",
				  "Labels": {
				    "build-date": "20171128",
				    "license": "GPLv2",
				    "name": "CentOS Base Image",
				    "vendor": "CentOS"
				  },
				  "Mounts": [],
				  "Names": [
				    "/clever_jennings"
				  ],
				  "NetworkSettings": {
				    "Networks": {
				      "bridge": {
				        "Aliases": null,
				        "EndpointID": "cc1aa301c873f42ad3c2027739e596929124663b73b066416b674c2846900d8d",
				        "Gateway": "172.17.0.1",
				        "GlobalIPv6Address": "",
				        "GlobalIPv6PrefixLen": 0,
				        "IPAMConfig": null,
				        "IPAddress": "172.17.0.2",
				        "IPPrefixLen": 16,
				        "IPv6Gateway": "",
				        "Links": null,
				        "MacAddress": "02:42:ac:11:00:02",
				        "NetworkID": "d8880cefc121ab187bd6cf2eaa0b2ea5f949ac3774bb5642d7632a31f6f0ef77"
				      }
				    }
				  },
				  "Ports": [],
				  "State": "exited",
				  "Status": "Exited (1) 5 weeks ago"
				}
        """
        protocol = self.get_query_argument('protocol')
        ipv4 = self.get_query_argument('ipv4', 'ipv4')
        port = self.get_query_argument('port')
        version = self.get_query_argument('version', '1.21')
        timeout = self.get_query_argument('timeout', 3)
        client = docker.Client(base_url=protocol+'://'+ipv4+':'+port, version=version, timeout=timeout)
        # 列出所有镜像
        for containers in client.containers(all = True):
            print('containers', json.dumps(containers, indent = 2, sort_keys=str ))
        res_dic = {'code': 200, 'text': '节点容器查询成功', 'res': None}
        self.write(json.dumps(res_dic))
        print(res_dic)
        return

    # @set_http_head
    # @post(
    #     _path="/docker_monarch/docker_node_version",
    #     _types=[str],
    #     _produces=mediatypes.APPLICATION_JSON)
    # def get_docker_node_version(self):
    #     """
    #     docker 节点 版本查询
    #     protocol + '://' + ipv4 + port == tcp://192.168.43.116:9527
    #
    #     测试
    #
    #     GET http://localhost:8080/docker_monarch/docker_node_version?
    #                                                         protocol=tcp&ipv4=192.168.43.116&port=9527&version=&timeout=
    #
    #     :return:
    #         {'Version': '1.12.6',
    #         'ApiVersion': '1.24',
    #         'GitCommit': 'ec8512b/1.12.6',
    #         'GoVersion': 'go1.8.3',
    #         'Os': 'linux',
    #         'Arch': 'amd64',
    #         'KernelVersion': '3.10.0-693.el7.x86_64',
    #         'BuildTime': '2017-12-11T16:08:42.362786068+00:00',
    #         'PkgVersion': 'docker-1.12.6-68.gitec8512b.el7.centos.x86_64'
    #         }
    #     """
    #     logger.info()
    #     protocol = self.get_query_argument('protocol')
    #     ipv4 = self.get_query_argument('ipv4', 'ipv4')
    #     port = self.get_query_argument('port')
    #     version = self.get_query_argument('version', '1.21')
    #     timeout = self.get_query_argument('timeout', 3)
    #     client = docker.Client(base_url=protocol+'://'+ipv4+':'+port, version=version, timeout=timeout)
    #     # 查看版本
    #     res_dic = {'res_data': client.version()}
    #
    #     self.write(json.dumps(res_dic))
    #     return
    #
    # @set_http_head
    # @delete(
    #     _path="/docker_monarch/docker_node_version",
    #     _types=[str],
    #     _produces=mediatypes.APPLICATION_JSON)
    # def get_docker_node_version(self):
    #     """
    #     docker 节点 版本查询
    #     protocol + '://' + ipv4 + port == tcp://192.168.43.116:9527
    #
    #     测试
    #
    #     GET http://localhost:8080/docker_monarch/docker_node_version?
    #                                                         protocol=tcp&ipv4=192.168.43.116&port=9527&version=&timeout=
    #
    #     :return:
    #         {'Version': '1.12.6',
    #         'ApiVersion': '1.24',
    #         'GitCommit': 'ec8512b/1.12.6',
    #         'GoVersion': 'go1.8.3',
    #         'Os': 'linux',
    #         'Arch': 'amd64',
    #         'KernelVersion': '3.10.0-693.el7.x86_64',
    #         'BuildTime': '2017-12-11T16:08:42.362786068+00:00',
    #         'PkgVersion': 'docker-1.12.6-68.gitec8512b.el7.centos.x86_64'
    #         }
    #     """
    #     logger.info()
    #     protocol = self.get_query_argument('protocol')
    #     ipv4 = self.get_query_argument('ipv4', 'ipv4')
    #     port = self.get_query_argument('port')
    #     version = self.get_query_argument('version', '1.21')
    #     timeout = self.get_query_argument('timeout', 3)
    #     client = docker.Client(base_url=protocol+'://'+ipv4+':'+port, version=version, timeout=timeout)
    #     # 查看版本
    #     res_dic = {'res_data': client.version()}
    #
    #     self.write(json.dumps(res_dic))
    #     return
    #
    # @set_http_head
    # @put(
    #     _path="/docker_monarch/docker_node_version",
    #     _types=[str],
    #     _produces=mediatypes.APPLICATION_JSON)
    # def get_docker_node_version(self):
    #     """
    #     docker 节点 版本查询
    #     protocol + '://' + ipv4 + port == tcp://192.168.43.116:9527
    #
    #     测试
    #
    #     GET http://localhost:8080/docker_monarch/docker_node_version?
    #                                                         protocol=tcp&ipv4=192.168.43.116&port=9527&version=&timeout=
    #
    #     :return:
    #         {'Version': '1.12.6',
    #         'ApiVersion': '1.24',
    #         'GitCommit': 'ec8512b/1.12.6',
    #         'GoVersion': 'go1.8.3',
    #         'Os': 'linux',
    #         'Arch': 'amd64',
    #         'KernelVersion': '3.10.0-693.el7.x86_64',
    #         'BuildTime': '2017-12-11T16:08:42.362786068+00:00',
    #         'PkgVersion': 'docker-1.12.6-68.gitec8512b.el7.centos.x86_64'
    #         }
    #     """
    #     logger.info()
    #     protocol = self.get_query_argument('protocol')
    #     ipv4 = self.get_query_argument('ipv4', 'ipv4')
    #     port = self.get_query_argument('port')
    #     version = self.get_query_argument('version', '1.21')
    #     timeout = self.get_query_argument('timeout', 3)
    #     client = docker.Client(base_url=protocol+'://'+ipv4+':'+port, version=version, timeout=timeout)
    #     # 查看版本
    #     res_dic = {'res_data': client.version()}
    #
    #     self.write(json.dumps(res_dic))
    #     return
    #
    # def get_services(self):
    #     pass
    #
    # def get_paths(self):
    #     pass
    #
    #
    # def get_handlers(self):
    #     pass